package com.fenghuajueli.module_user.network.repository

import android.content.Context
import com.blankj.utilcode.util.LogUtils
import com.fenghuajueli.libbasecoreui.user.UserInfoUtils
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils
import com.fenghuajueli.module_user.dialog.RefundTipDialog
import com.fenghuajueli.module_user.network.retrofit.UserNetwork

/**
 * @Author: sz_ijj
 * @Date: 2024-04-28 14:33
 **/
object RefundRepository {
    private const val REFUND_COUNT = "RefundCount"
    private const val REFUND_CONSUMPTION_COUNT = "REFUND_CONSUMPTION_COUNT"

    @JvmStatic
    fun showRefundTipDialog(context: Context) {
        if (isConsumption()) {
            RefundTipDialog(context).show()
        }
    }

    @JvmStatic
    fun isConsumption() = MmkvUtils.get(REFUND_COUNT, 0) !=
            MmkvUtils.get(REFUND_CONSUMPTION_COUNT, 0)

    @JvmStatic
    fun saveRefundCount(count:Int) {
        MmkvUtils.save(REFUND_COUNT, count)
    }

    @JvmStatic
    fun saveRefundConsumptionCount() {
        MmkvUtils.save(REFUND_CONSUMPTION_COUNT, MmkvUtils.get(REFUND_COUNT,0))
    }

    suspend fun initRefundData() {
        runCatching {
            val result =
                UserNetwork.getRefundResult(UserInfoUtils.getInstance().userInfoEntity.token)
            LogUtils.d("退款结果数据:$result")
            when (result.code) {
                200 -> result.data.data_list
                else -> emptyList()
            }
        }.onFailure {
            LogUtils.e(it.message)
        }.onSuccess { result ->
            saveRefundCount(result.size)
        }
    }
}